#include "PrecompiledHeader.h"
#include "StatisticsHud.h"

/*--------------------------------------------------------------------------*/

StatisticsHud::StatisticsHud() :
	ICriticalSection(2)
{
	AssertModule(g_Shell);
	this->var_label_enabled = g_Shell->AddVar("label_enabled", 1);
#ifdef VL_RENDER_STATISTICS
	this->var_statistics_enabled = g_Shell->AddVar("statistics_enabled", 1);
#endif

	AssertModule(g_Resources);
	g_Resources->Get("default", &this->res_default_small);
}

/*--------------------------------------------------------------------------*/

StatisticsHud::~StatisticsHud()
{

}

/*--------------------------------------------------------------------------*/

void StatisticsHud::OnHudUpdate()
{

}

/*--------------------------------------------------------------------------*/

void StatisticsHud::OnHudRender()
{
	this->DrawAllLabels();
}

/*--------------------------------------------------------------------------*/

void StatisticsHud::DrawAllLabels()
{
	float y = g_Settings->GetResolutionf().y - 10.0f;

	this->Lock(Labels);

	for (std::map<const std::string, std::string>::iterator iter = this->m_labels.begin(); iter != this->m_labels.end(); ++iter)
	{
		g_Renderer->DrawText2D(this->res_default_small, iter->second, glm::vec2(10.0f, y -= this->res_default_small->GetSize()), Color::White);
	}

	this->m_labels.clear();
	this->Unlock(Labels);

#ifdef VL_RENDER_STATISTICS

	if (this->var_statistics_enabled->GetBool())
	{
		this->Lock(Statistics);

		for (std::map<const char*, int>::iterator iter = this->m_statistics.begin(); iter != this->m_statistics.end(); ++iter)
		{
			std::stringstream ss;
			ss << (iter->first) << " = " << iter->second;

			g_Renderer->DrawText2D(this->res_default_small, ss.str(), glm::vec2(10.0f, y -= this->res_default_small->GetSize()), Color::White);
		}

		this->m_statistics.clear();

		this->Unlock(Statistics);
	}

#endif
}

/*--------------------------------------------------------------------------*/

#ifdef VL_RENDER_STATISTICS
void StatisticsHud::IncrementStatistic(const char* statistics, unsigned int value)
{
	if (this->var_statistics_enabled->GetBool())
	{
		this->Lock(Statistics);
		std::map<const char*, int>::iterator iter = this->m_statistics.find(statistics);

		if (iter == this->m_statistics.end())
		{
			this->m_statistics[statistics] = value;
		}
		else
		{
			iter->second = iter->second + value;
		}

		this->Unlock(Statistics);
	}
}
#endif

/*--------------------------------------------------------------------------*/

#ifdef VL_RENDER_STATISTICS
void StatisticsHud::DecrementStatistic(const char* statistics)
{
	if (this->var_statistics_enabled->GetBool())
	{
		this->Lock(Statistics);
		std::map<const char*, int>::iterator iter = this->m_statistics.find(statistics);

		if (iter == this->m_statistics.end())
		{
			this->m_statistics[statistics] = -1;
		}
		else
		{
			iter->second = iter->second - 1;
		}

		this->Unlock(Statistics);
	}
}
#endif

/*--------------------------------------------------------------------------*/

void StatisticsHud::SetLabel(const std::string& name, const std::string& val)
{
	if (this->var_label_enabled->GetBool())
	{
		this->Lock(Labels);
		this->m_labels[name] = val;
		this->Unlock(Labels);
	}
}

/*--------------------------------------------------------------------------*/